package com.itheima.service.impl;

import com.itheima.dao.FeedbackDao;
import com.itheima.pojo.Feedback;
import com.itheima.service.FeedbackService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.vo.FeedbackVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackDao feedbackDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 批量删除反馈
     *
     * @param feedbackVOList
     *@author itheima
     */
    @Override
    public void deleteBatch(List<FeedbackVO> feedbackVOList) {
        Feedback feedback = new Feedback();

        if (feedbackVOList != null || feedbackVOList.size() > 0) {
            for (FeedbackVO feedbackVO : feedbackVOList) {
                if (feedbackVO.getUid() != null && !"".equals(feedbackVO.getUid())) {
                    feedback.setUid(feedbackVO.getUid());
                }
                feedbackDao.deleteByPrimaryKey(feedback.getUid());
            }
        }
    }

    /**
     * 编辑反馈
     *
     * @param feedbackVO
     *@author itheima
     */
    @Override
    public void edit(FeedbackVO feedbackVO) {
        Feedback feedback = new Feedback();
        feedback.setContent(feedbackVO.getContent());
        feedback.setFeedbackStatus(feedbackVO.getFeedbackStatus());
        feedback.setReply(feedbackVO.getReply());
        feedback.setStatus(feedbackVO.getStatus());
        feedback.setTitle(feedbackVO.getTitle());
        feedback.setUid(feedbackVO.getUid());
        feedback.setUserUid(feedbackVO.getUserUid());

        feedbackDao.updateByPrimaryKeySelective(feedback);
    }

    /**
     * 获取反馈列表
     *@param feedbackVO
     * @return java.util.List<com.itheima.pojo.feedback>
     *@author itheima
     */
    @Override
    public List<Feedback> getList(FeedbackVO feedbackVO) {
        Feedback feedback = new Feedback();
        Example example = new Example(feedback.getClass());
        List<Feedback> feedbackList = feedbackDao.selectByExample(example);
        return feedbackList;
    }

    @Autowired
    private IdWorker idWorker;

    @Override
    public List<Feedback> findByUserUid(String userUid) {
        Map<String,Object> map=new HashMap<>();
        map.put("userUid",userUid);
        Example example = this.createExample(map);
        List<Feedback> feedbackList = feedbackDao.selectByExample(example);
        return feedbackList;
    }

    @Override
    public void delete(String accessToken) {
        redisTemplate.delete(accessToken);
    }

    @Override
    public List<Feedback> getFeedbackList() {
        List<Feedback> list = feedbackDao.selectAll();
        return list;
    }

    @Override
    public void addFeedback(FeedbackVO feedbackVO) {
        Feedback feedback = new Feedback();
        feedback.setUid(String.valueOf(idWorker.nextId()));
        feedback.setUserUid(feedbackVO.getUserUid());
        feedback.setContent(feedbackVO.getContent());
        feedback.setStatus(feedbackVO.getStatus());
        feedback.setCreateTime(DateUtils.getNowTime());
        feedback.setUpdateTime(DateUtils.getNowTime());
        feedback.setTitle(feedbackVO.getTitle());
        feedback.setFeedbackStatus(feedbackVO.getFeedbackStatus());
        feedback.setReply(feedbackVO.getReply());
        /**
         * UserName 未获取
         */
        feedback.setAdminUid(feedbackVO.getUserUid());
        int insert = feedbackDao.insert(feedback);
        if (insert < 1){
            throw new RuntimeException("提交反馈失败");
        }
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Feedback.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 唯一uid
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andEqualTo("uid",searchMap.get("uid"));
            }
            // 用户uid
            if(searchMap.get("userUid")!=null && !"".equals(searchMap.get("userUid"))){
                criteria.andEqualTo("userUid",searchMap.get("userUid"));
            }
            // 反馈的内容
            if(searchMap.get("content")!=null && !"".equals(searchMap.get("content"))){
                criteria.andLike("content","%"+searchMap.get("content")+"%");
            }

            // 状态
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

            // 创建时间
            if(searchMap.get("createTime")!=null){
                criteria.andEqualTo("createTime",searchMap.get("createTime"));
            }

            // 更新时间
            if(searchMap.get("updateTime")!=null){
                criteria.andEqualTo("updateTime",searchMap.get("updateTime"));
            }

            // 标题
            if(searchMap.get("title")!=null && !"".equals(searchMap.get("title"))){
                criteria.andEqualTo("title",searchMap.get("title"));
            }

            // 回复
            if(searchMap.get("reply")!=null && !"".equals(searchMap.get("reply"))){
                criteria.andEqualTo("reply",searchMap.get("reply"));
            }

            // 反馈状态： 0：已开启  1：进行中  2：已完成  3：已拒绝
            if(searchMap.get("feedbackStatus")!=null){
                criteria.andEqualTo("feedbackStatus",searchMap.get("feedbackStatus"));
            }

            // 管理员uid
            if(searchMap.get("adminUid")!=null && !"".equals(searchMap.get("adminUid"))){
                criteria.andEqualTo("adminUid",searchMap.get("adminUid"));
            }

        }
        return example;
    }
}
